<?php
/**
 * StatusNet, the distributed open-source microblogging tool
 *
 * Add a new group
 *
 * PHP version 5
 *
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category  Group
 * @package   StatusNet
 * @author    Evan Prodromou <evan@status.net>
 * @author    Sarven Capadisli <csarven@status.net>
 * @copyright 2008-2009 StatusNet, Inc.
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link      http://status.net/
 */

if (!defined('STATUSNET') && !defined('LACONICA')) {
	exit(1);
}

/**
 * Add a new group
 *
 * This is the form for adding a new group
 *
 * @category Group
 * @package  StatusNet
 * @author   Evan Prodromou <evan@status.net>
 * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link     http://status.net/
 */

class NewamwayproductAction extends Action {
	var $msg;

	function title() {
		return _('New Product');
	}

	/**
	 * Prepare to run
	 */

	function prepare($args) {
		parent::prepare($args);

		if (!common_config('inboxes','enabled')) {
			$this->serverError(_('Inboxes must be enabled for groups to work'));
			return false;
		}

		if (!common_logged_in()) {
			$this->clientError(_('You must be logged in to create a product.'));
			return false;
		}

		return true;
	}

	/**
	 * Handle the request
	 *
	 * On GET, show the form. On POST, try to save the group.
	 *
	 * @param array $args unused
	 *
	 * @return void
	 */

	function handle($args) {
		parent::handle($args);
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$this->trySave();
		} else {
			$this->showForm();
		}
	}

	function showForm($msg=null) {
		$this->msg = $msg;
		$this->showPage();
	}

	function showContent() {
		//		$form = new AmwayProductEditForm($this);
		//		$form->show();

		if ($this->amwayproduct) {
			$id = $this->amwayproduct->id;
			$product_code = $this->amwayproduct->product_code;
			$product_name = $this->amwayproduct->product_name;
			$pro_line_id = $this->amwayproduct->pro_line_id;
			$description = $this->amwayproduct->description;
			$effects = $this->amwayproduct->$effects;
		} else {
			$id = '';
			$product_code = '';
			$product_name = '';
			$pro_line_id = '';
			$description = '';
			$effects = '';
		}
		$this->elementStart('form', array('enctype' => 'multipart/form-data',
                                          'method' => 'post',
                                          'id' => 'form_settings_avatar',
                                          'class' => 'form_settings',
                                          'action' =>
		common_local_url('newamwayproduct')));
		$this->hidden('token', common_session_token());
		$this->elementStart('ul', 'form_data');
		$this->elementStart('li');
		$this->hidden('amwayproductid', $id);
		$this->input('product_code', _('Amway Product Code'),
		($this->arg('product_code')) ? $this->arg('product_code') : $product_code,
		_('1-64 lowercase letters or numbers, no punctuation or spaces'));
		$this->elementEnd('li');

		$this->elementStart('li');
		$this->input('product_name', _('Amway Product Name'),
		($this->arg('product_name')) ? $this->arg('product_name') : $product_name);
		$this->elementEnd('li');

		
		$this->elementStart('li');
		$this->input('current_price', _('Current price'),
		($this->arg('current_price')) ? $this->arg('current_price') : $current_price);
		$this->elementEnd('li');
		
		$this->elementStart('li');

		$this->textarea('description', _('Description'),
		$this->trimmed('description'),
		_('Describe product description in 140 chars'));	
		$this->elementEnd('li');

		$this->elementStart('li');

		$this->textarea('effects', _('Effects'),
		$this->trimmed('effects'),
		_('Describe product effect in 140 chars'));			
		$this->elementEnd('li');

		$this->elementStart('li', array ('id' => 'settings_attach'));
		 $this->element('label', array('for' => 'amwayproductfile1'), _('Image'));
		$this->element('input', array('name' => 'amwayproductfile',
                                      'type' => 'file',
                                      'id' => 'amwayproductfile'));
		$this->element('input', array('name' => 'MAX_FILE_SIZE',
                                      'type' => 'hidden',
                                      'id' => 'MAX_FILE_SIZE',
                                      'value' => ImageFile::maxFileSizeInt()));
		$this->elementEnd('li');
		$this->elementEnd('ul');
		$this->elementStart('ul', 'form_actions');
		$this->elementStart('li');	
		$this->submit('upload', _('Save'));
		$this->elementEnd('li');
		$this->elementEnd('ul');
		$this->elementEnd('form');

	}

	function showPageNotice() {
		if ($this->msg) {
			$this->element('p', 'error', $this->msg);
		} else {
			$this->element('p', 'instructions',
			_('Use this form to create a new product.'));
		}
	}

	function trySave() {
		$product_code    = $this->trimmed('product_code');
		$product_name    = $this->trimmed('product_name');
		$description = $this->trimmed('description');
		
		$effects = $this->trimmed('effects');
		$current_price = $this->trimmed('current_price');

			


		$amway_product = new Amway_product();

		$amway_product->query('BEGIN');
		$amway_product->product_code    = $product_code;
		$amway_product->product_name    = $product_name;
		$amway_product->description = $description;
		$amway_product->effects = $effects;
		$amway_product->is_active = 1;
		$amway_product->current_price = $current_price;
		try {
			$imagefile = ImageFile::fromUpload('amwayproductfile');
		} catch (Exception $e) {
			$this->showForm($e->getMessage());
			return;
		}

		$filename = Amway_product::filename($this->group->id,
		image_type_to_extension($imagefile->type),
		null,
                                     'group-temp-'.common_timestamp());

		$filepath = Amway_product::path($filename);

		move_uploaded_file($imagefile->filepath, $filepath);

		$amway_product->height = $imagefile->height;
		$amway_product->width = $imagefile->width;
		$amway_product->original = true;
		$amway_product->mediatype = image_type_to_mime_type($imagefile->type);
		$amway_product->url = Amway_product::url($filename);
		$amway_product->filename = $filename;
		$amway_product->created_at     = common_sql_now();

		$result = $amway_product->insert();

		//        var_dump($result);
		//        exit();

		if (!$result) {
			common_log_db_error($amway_product, 'INSERT', __FILE__);
			$this->serverError(_('Could not create group.'));
		} else {


		}
		$url = common_local_url('newamwayproduct');
		$amway_product->query('COMMIT');
		common_redirect($url, 303);
	}
}

